package cn.humaohua.study.yanmo_design_pattern.ch20.v2;

import org.apache.commons.lang3.StringUtils;

public class AuthorizationFlyweight implements Flyweight {

    private String securityEntity;
    private String permit;

    public AuthorizationFlyweight(String state) {
        if (StringUtils.contains(state, ",")) {
            String[] arr = state.split(",");
            setSecurityEntity(arr[0]);
            setPermit(arr[1]);
        } else {
            throw new IllegalArgumentException("state不能为空");
        }
    }

    @Override
    public boolean match(String securityEntity, String permit) {
        return StringUtils.equals(this.securityEntity, securityEntity) && StringUtils.equals(this.permit, permit);
    }

    @Override
    public void add(Flyweight flyweight) {
        throw new UnsupportedOperationException("对象不支持这个功能");
    }

    public String getSecurityEntity() {
        return securityEntity;
    }

    public void setSecurityEntity(String securityEntity) {
        this.securityEntity = securityEntity;
    }

    public String getPermit() {
        return permit;
    }

    public void setPermit(String permit) {
        this.permit = permit;
    }
}
